package tablemodel;

import Entities.Reserva;
import ejb.Condicional;
import ejb.Orden;
import ejb.ReservaFacadeLocal;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import org.primefaces.model.LazyDataModel;
import org.primefaces.model.SortOrder;
import static org.primefaces.model.SortOrder.ASCENDING;
import static org.primefaces.model.SortOrder.DESCENDING;

public class ReservaLazyModel extends LazyDataModel<Reserva> {

    ReservaFacadeLocal reservaFacade = lookupReservaFacadeLocal();

    public ReservaLazyModel(ReservaFacadeLocal reservaFacade) {
    }

    @Override
    public Reserva getRowData(String rowKey) {
        return null;
    }

    @Override
    public Object getRowKey(Reserva reserva) {
        return reserva.getId();
    }

    @Override
    public List<Reserva> load(int first, int pageSize, String sortField, SortOrder sortOrder, Map<String, String> filters) {
        List<Reserva> data;
        try {
            reservaFacade.establecerOrdenacion(null, Orden.NINGUNO);
            if (sortField != null && !"".equals(sortField)) {
                switch (sortOrder) {
                    case ASCENDING:
                        reservaFacade.establecerOrdenacion(sortField, Orden.ASC);
                        break;
                    case DESCENDING:
                        reservaFacade.establecerOrdenacion(sortField, Orden.DESC);
                        break;
                }
            }
            List<Condicional> condicionales = new ArrayList<>();
            if (!filters.isEmpty()) {
                for (Iterator<String> it = filters.keySet().iterator(); it.hasNext();) {
                    String filterProperty = it.next();
                    try {
                        String valor = filters.get(filterProperty);
                        switch (filterProperty.toLowerCase()) {
                            case "id": case "rol": case "activo":
                                condicionales.add(new Condicional(filterProperty, Integer.valueOf(valor), "="));
                                break;
                            default:
                                condicionales.add(new Condicional(filterProperty, valor, "LIKE"));
                        }
                    } catch (Exception e) {
                    }
                }
            }
            int dataSize;
            if (condicionales.isEmpty()) {
                data = reservaFacade.buscarRango(new int[]{first, first + pageSize});
                dataSize = reservaFacade.contar();
            } else {
                data = reservaFacade.buscarConParametros(condicionales, null, new int[]{first, first + pageSize});
                dataSize = reservaFacade.contarConParametros(condicionales, null);
            }
            this.setRowCount(dataSize);
            return data;
        } catch (Exception ex) {
             Logger.getLogger(ReservaLazyModel.class.getName()).log(Level.SEVERE, null, ex);
            return data = new ArrayList<>();
        }
    }

    private ReservaFacadeLocal lookupReservaFacadeLocal() {
        try {
            Context c = new InitialContext();
            return (ReservaFacadeLocal) c.lookup("java:global/SGR/SGR-ejb/ReservaFacade!ejb.ReservaFacadeLocal");
        } catch (NamingException ne) {
            Logger.getLogger(getClass().getName()).log(Level.SEVERE, "exception caught", ne);
            throw new RuntimeException(ne);
        }
    }
}
